const app = getApp();
Page({
  data: {
    navIndex: 'all',
    nav: [{
        title: '领券',
        value: 'all',
      }, {
        title: '未使用',
        value: 'unuse',
      }, {
        title: '已使用',
        value: 'used',
      }, {
        title: '已过期',
        value: 'expried',
      }],
    list: []
  },
  onLoad: function (options) {
    this.setData({
      from: options.from || ''
    })
  },
  onShow: function () {
    let that = this;
    
    app.request('coupon/index',{},'GET',false,function(res){
      that.setData({
        pageShow: true,
        all: res.data.data.list.all,
        list: res.data.data.list.myCoupon,
      })
    })
  },
  navChoose: function (e) {
    let that = this;
    let value = e.currentTarget.dataset.value;
    that.setData({
      navIndex: value,
    })
  },
  getTap(e){
    let that = this;
    let coupon_id = e.currentTarget.dataset.id;
    app.request('coupon/getCoupon', { coupon_id },'GET',false,function(res){
      wx.showToast({
        title: res.data.msg,
      })
      let index = e.currentTarget.dataset.index;
      let all = that.data.all;
      let list = that.data.list;
      list['unuse'].push(all[index]);
      all[index]['hasGet'] = true;
      that.setData({
        all: all,
        list: list,
      })
    })
  },
  couponTap(e){
    let that = this;
    if(that.data.from){
      wx.switchTab({
        url: '/pages/index/index',
      })
      return false
    }
    let index = e.currentTarget.dataset.index;
    let idx = e.currentTarget.dataset.idx;
    let list = that.data.list;
    wx.setStorageSync('couponChoose', list[index][idx]); 
  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {
    
  },
  onShareAppMessage(){
    return {
      path: '/pages/index/index'
    }
  }
})